package com.kgc.sbt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.kgc.sbt.api.TrackApi;
import com.kgc.sbt.config.annotation.RequestPermission;
import com.kgc.sbt.domain.Track;
import com.kgc.sbt.util.RedisUtils;
import com.kgc.sbt.util.RequestResult;
import com.kgc.sbt.util.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created on 2021/6/23 0023.
 * <p>
 * Author: px
 * <p>
 * Description: 足迹控制入口
 */
@RestController
@Api(tags = "足迹操作入口，版本号：1.0.0")
@Slf4j
@RequestPermission
public class TrackController {

    @Autowired
    private RedisUtils redisUtils;

    @Reference
    private TrackApi trackApi;



    /**
     * @Author px
     * @Date 2021/6/28 0028
     * @Param
     * @Description: 获取足迹列表
     **/
    @GetMapping("/trackList")
    @ApiOperation(value = "查询足迹", notes = "从token中获取userId，查询足迹列表")
    public RequestResult<List<Track>> queryTrack(HttpServletRequest request){
        // 校验权限，是否已经登录
        String token = request.getHeader("token");

        // 校验token是否有效
        Integer userId = (Integer) redisUtils.get(token);
        log.info("------ userId:" + userId);
        return ResultBuildUtil.success(trackApi.queryTrack(userId));
    }




    /**
     * @Author px
     * @Date 2021/6/23 0023
     * @Param [goodsId]
     * @Description: 添加足迹
     **/
    @GetMapping("/addTrack")
    @ApiOperation(value = "添加足迹", notes = "输入商品id，插入足迹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",required = true,value = "商品id")
    })
    public RequestResult<String> addTrackById(@RequestParam Integer goodsId, HttpServletRequest request){

        // 校验权限，是否已经登录
        String token = request.getHeader("token");

        // 校验token是否有效
        Integer userId = (Integer) redisUtils.get(token);
        log.info("------ userId:" + userId);

        trackApi.addTrack(userId, goodsId);
        return ResultBuildUtil.success();

    }




    /**
     * @Author px
     * @Date 2021/6/23 0023
     * @Param [goodsId]
     * @Description: 删除足迹
     **/
    @GetMapping("/delTrack")
    @ApiOperation(value = "删除足迹", notes = "输入商品id，删除足迹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",required = true,value = "商品id")
    })
    public RequestResult<String> delTrackById(@RequestParam Integer goodsId, HttpServletRequest request){

        // 校验权限，是否已经登录
        String token = request.getHeader("token");

        // 校验token是否有效
        Integer userId = (Integer) redisUtils.get(token);
        log.info("------ userId:" + userId);

        if (trackApi.delTrack(userId, goodsId)){
            return ResultBuildUtil.success();
        }
        return ResultBuildUtil.fail("7001","删除失败，不存在该条足迹..");

    }




}
